package org.bigjava.novelplatform.common.utils;


import org.bigjava.novelplatform.common.exception.ExceptionCodeMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回值
 * @author ykh
 * @create 2022-03-20 16:18
 */
public class R extends HashMap<String,Object> {

    public R() {
        put("code", 0);
        put("msg", "success");
    }

    public R setData(Object data) {
        return put("data", data);
    }

    private static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }
    public static R error(ExceptionCodeMessage ex) {
        return error(ex.getCode(), ex.getMessage());
    }

    public static R ok(Map<String, Object> map) {
        R r = ok();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public static R ok(Object data) {
        return ok().setData(data);
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }



}
